package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.border.Border;

import presentation.PMain;

/**
 * Launch the application.
 *
 *<P>Perform tasks in this order :
 *<ul>
 * <li>log basic system information 
 * <li>promptly show a splash screen upon startup
 * <li>show the main screen
 * <li>remove the splash screen once the main screen is shown
 *</ul>
 *
 * These tasks are performed in a thread-safe manner.
 */
public final class Launcher { 
	private static Splash fSplashScreen;
	private static final String SPLASH_IMAGE = "img/splash.gif";
	/**
	 * Launch the application and display the main window.
	 *
	 * @param aArgs are ignored by this application, and may take any value.
	 */
	public static void main (String[] aArgs) {

		/*
		 * Implementation Note:
		 *
		 * Note that the launch thread of any GUI application is in effect an initial 
		 * worker thread - it is not the event dispatch thread, where the bulk of processing
		 * takes place. Thus, once the launch thread realizes a window, then the launch 
		 * thread should almost always manipulate such a window through 
		 * EventQueue.invokeLater. (This is done for closing the splash 
		 * screen, for example.)
		 */

		showSplashScreen();
		showMainWindow();
		EventQueue.invokeLater( new SplashScreenCloser() );
	}

	/**
	 * Show a simple graphical splash screen, as a quick preliminary to the main screen.
	 */
	private static void showSplashScreen(){
		fSplashScreen = new Splash(SPLASH_IMAGE);
		fSplashScreen.splash();
	}

	/**
	 * Display the main window of the application to the user.
	 */
	private static void showMainWindow(){
		JFrame frame = new JFrame("SDU vikings Liveview");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

		Image image = null;
		try {
			image = ImageIO.read(new File("img/icon.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		frame.setIconImage(image);

		try {
			UIManager.getDefaults().put("TabbedPane.contentAreaColor",Color.white);
			UIManager.getDefaults().put("TabbedPane.highlight",Color.red);
			UIManager.getDefaults().put("TabbedPane.selected",Color.white);
			UIManager.put("ToolTip.background", Color.white);
			Border border = BorderFactory.createLineBorder(Color.gray);
			UIManager.put("ToolTip.border", border);
			UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
		} catch (Exception e) {
			// Not handled.
		}

		frame.getContentPane().add(new PMain());
		frame.setResizable(true);
		frame.setSize(1024,768);
		frame.setMinimumSize(new Dimension(1024, 768));
		frame.setLocation(dim.width/2-512,dim.height/2-384);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	/** 
	 * Removes the splash screen. 
	 *
	 * Invoke this <tt>Runnable</tt> using 
	 * <tt>EventQueue.invokeLater</tt>, in order to remove the splash screen
	 * in a thread-safe manner.
	 */
	private static final class SplashScreenCloser implements Runnable {
		public void run(){
			fSplashScreen.dispose();
		}
	}
} 
